package com.das.mechanic_base.utils

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate

class LanguageLayoutInflaterFactory(val activity: Activity) : LayoutInflater.Factory2 {

    private val APP_KEY = "http://schemas.android.com/apk/res-auto"

    private val ANDROID_KEY = "http://schemas.android.com/apk/res/android"

    //sdk的activity使用的布局生成器
    private val delegate: AppCompatDelegate by lazy(LazyThreadSafetyMode.NONE) { AppCompatDelegate.create(activity, null) }

    //获取默认的createView方法,可以在这里判断并适配换肤框架等
    fun checkAndCreateView(parent: View?, name: String?, context: Context, attrs: AttributeSet): View? {
        return when (activity) {
            is AppCompatActivity -> activity.delegate.createView(parent, name, context, attrs)
            else -> delegate.createView(parent, name, context, attrs)
        }
    }

    override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
        return checkAndReturnView(name, context, attrs, checkAndCreateView(parent, name, context, attrs))
    }

    override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
        return null
    }

    fun checkAndReturnView(name: String, context: Context, attrs: AttributeSet, view: View?): View? {
        val view = view ?: try {
            createViewGroup(name, context, attrs)
        } catch (e: Exception) {
            e.toString()
            null
        } ?: return null

        handlerXmlText(view, attrs)
        return view
    }

    private fun handlerXmlText(view: View, attrs: AttributeSet) {
        if (view is TextView) {
            var value = getStringValue(attrs, true, "text")

            if (value != null)
                view.text = value
            value = getStringValue(attrs, true, "hint")
            if (value != null)
                view.hint = value
        }
        //下面两个是自定义View使用了@string/
//        if (view is MemberButtonView) {
//            getStringValue(attrs, false, "text_content")?.let { view :: changeText }
//        }
    }

    private fun Int.toText() = activity.getString(this)

    private fun getStringValue(attrs: AttributeSet, isAndroidSystem: Boolean, valueName: String): String? {
        val value = attrs.getAttributeValue(if (isAndroidSystem) ANDROID_KEY else APP_KEY, valueName)
        if (value?.startsWith("@") == true)
            try {
                return value.substring(1).toIntOrNull()?.toText()
            } catch (e: Exception) {
                e.toString()
            }
        return null
    }

    private fun createViewGroup(name: String, context: Context, attrs: AttributeSet): View? {
        return when (name) {
            //下面两个是自定义View使用了@string/
            //且下面两个是ViewGroup,如果是View则会在delegate中就创建完成
//            "om.das.technician.widget.MemberButtonView" -> MemberButtonView(context, attrs)
            else -> null
        }
    }
}